package abc.base;

import abc.filter.RequestMatchSchemaFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class BllFactory {
    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    RequestMatchSchemaFilter requestMatchSchemaFilter;

    public IBll getBll(String table) {
        return (IBll) applicationContext.getBean(table+"Bll");
    }

    public <T extends AbcBaseController> IBll getBll(Class<T> tClass) {
        String table = requestMatchSchemaFilter.getTableName(tClass);
        return getBll(table);
    }

    public <T extends AbcBaseController> IBll getBll(T controller) {
        return getBll(controller.getClass());
    }

    public <T extends BaseModel> IBll getBll(T model) {
        String modelName = model.getClass().getSimpleName();
        String table = modelName.substring(0, modelName.length()-"Model".length());
        return getBll(table);
    }
}
